//---------------------------------------------------------------------------
#pragma hdrstop
#include "ColoredPositionClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
CColoredPosition::CColoredPosition(TCanvas* canvas, int x, int y) : CObject(canvas, x, y) {
	name = position_name + " " + IntToStr(namecount++);
	number = count++;
	description = "";
	markcount = 0;
	canvas->Font->Name = main_font;
	canvas->Font->Size = radius / 3 * 2;
}
//---------------------------------------------------------------------------
void CColoredPosition::show() {
	int xe = x + radius, ye = y, tmp;
	int xb = xe, yb = ye;
	double alfa = 0, alfa0 = 0;
	canvas->Pen->Color = main_clFont;
	canvas->Font->Color = main_clNFont;
	canvas->TextOutA(x - 5*radius / 3 - 2, y - 4*radius / 3 - 2, number);
	canvas->Font->Color = main_clFont;
	if (markcount != 0) {
		canvas->MoveTo(x, y);
		canvas->LineTo(xb, yb);
		for (int i = 0; i < marks.size(); i++) {
			canvas->Brush->Style = bsClear;
			canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
			alfa0 = alfa;
			alfa += 360.0*((marks[i].markcount*1.0) / (markcount*1.0));
			if (alfa > 360)
				alfa = 360;
			if (alfa - alfa0 == 360) {
				canvas->Brush->Style = bsSolid;
				canvas->Brush->Color = mark_colors[marks[i].color];
				canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
				break;
			}
			else {
				xe = x + radius * cos(3.14 / 180 * alfa);
				ye = y - radius * sin(3.14 / 180 * alfa);
				if (alfa == 180)
					ye += 1;
				if (alfa == 270)
					xe += 1;
				canvas->MoveTo(x, y);
				canvas->LineTo(xe, ye);
				canvas->Brush->Style = bsSolid;
				canvas->Brush->Color = mark_colors[marks[i].color];
				canvas->FloodFill(x + radius / 2*cos(3.14 / 180*(alfa + alfa0) / 2), y - radius / 2*sin(3.14 / 180*(alfa + alfa0) / 2), main_clFont, fsBorder);
				xb = xe;
				yb = ye;
			}
		}
	}
	canvas->Pen->Color = main_clBorder;
	if (markcount != 0)
		canvas->Brush->Style = bsClear;
	else
		canvas->Brush->Style = bsSolid;
	canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
	canvas->Brush->Color = clWhite;
	canvas->Brush->Style = bsSolid;
}
//---------------------------------------------------------------------------
void CColoredPosition::hide() {
	canvas->Pen->Color = clWhite;
	canvas->Font->Color = clWhite;
	canvas->TextOutA(x - 5*radius / 3 - 2, y - 4*radius / 3 - 2, number);
	canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
}
//---------------------------------------------------------------------------
bool CColoredPosition::isSelect(int xm, int ym) {
	double rtec = sqrt((xm - x) * (xm - x) + (ym - y) * (ym - y));
	if (rtec <= radius)
		return true;
	else
		return false;
}
//---------------------------------------------------------------------------
void CColoredPosition::setRadius(int radius) {
	if (radius < 10)
		radius = 10;
	if (radius > 19)
		radius = 19;
	this->radius = radius;
	canvas->Font->Size = radius / 3 * 2;
}
//---------------------------------------------------------------------------
void CColoredPosition::setMarkCount(int color, int markcount) {
	for (int i = 0; i < marks.size(); i++)
		if (color == marks[i].color) {
			this->markcount -= marks[i].markcount;
			if (markcount > 99 && markcount != 100)
				markcount = 99;
			marks[i].markcount = markcount;
			this->markcount += markcount;
		}
}
//---------------------------------------------------------------------------
void CColoredPosition::addColor(int color) {
	bool check = true;
	for (int i = 0; i < marks.size(); i++)
		if (color == marks[i].color) {
			check = false;
			break;
		}
	if (check) {
		mark m;
		m.color = color;
		m.markcount = 0;
		marks.push_back(m);
	}
}
//---------------------------------------------------------------------------
void CColoredPosition::delColor(int color) {
	for (int i = 0; i < marks.size(); i++)
		if (color == marks[i].color) {
			markcount -= marks[i].markcount;
			for (int j = i; j < marks.size() - 1; j++) {
				marks[j].color = marks[j + 1].color;
				marks[j].markcount = marks[j + 1].markcount;
			}
			marks.pop_back();
			break;
		}
}
//---------------------------------------------------------------------------
vector <mark> CColoredPosition::getMarkCount() {
	return marks;
}
//---------------------------------------------------------------------------
UnicodeString CColoredPosition::save() {
	int size = marks.size();
	UnicodeString str = "", n = name, d = description;
	for (int i = 1; i <= n.Length(); i++)
		if (n[i] == ' ')
			n[i] = '`';
	for (int i = 1; i <= d.Length(); i++)
		if (d[i] == ' ')
			d[i] = '`';
	str += IntToStr(x) + " " + IntToStr(y) + " ";
	str += IntToStr(size) + " ";
	for (int i = 0; i < size; i++)
         str += IntToStr(marks[i].color) + " " + IntToStr(marks[i].markcount) + " ";
	if (d == "")
		d = "EMPTY_DESCRIPTION";
	str += n + " " + d;
	return str;
}
//---------------------------------------------------------------------------
CColoredPosition::~CColoredPosition() {
	count--;
}
//---------------------------------------------------------------------------
